// SettingsFrame.java
package com.ren.frame;

import com.ren.config.AutoStartupManager;
import com.ren.constants.AppConstants;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 设置窗口类
 * 用于管理应用的各种设置选项
 */
public class SettingsFrame extends JFrame {

    private JCheckBox autoStartCheckBox;
    private JButton saveButton;
    private JButton cancelButton;

    public SettingsFrame(Frame parent) {
        super(AppConstants.SETTING);
        initializeComponents();
        setupLayout();
        loadSettings();

        // 设置窗口属性
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setSize(400, 300);
        setLocationRelativeTo(parent);
        setResizable(false);
    }

    /**
     * 初始化组件
     */
    private void initializeComponents() {
        autoStartCheckBox = new JCheckBox(AppConstants.AUTO_STARTUP);

        saveButton = new JButton("保存");
        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                saveSettings();
            }
        });

        cancelButton = new JButton("取消");
        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
    }

    /**
     * 设置布局
     */
    private void setupLayout() {
        setLayout(new BorderLayout());

        // 创建主面板
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
        mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        // 添加设置选项
        JPanel autoStartPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        autoStartPanel.add(autoStartCheckBox);
        mainPanel.add(autoStartPanel);

        // 添加按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        buttonPanel.add(saveButton);
        buttonPanel.add(cancelButton);

        mainPanel.add(Box.createVerticalGlue());
        mainPanel.add(buttonPanel);

        add(mainPanel, BorderLayout.CENTER);
    }

    /**
     * 加载当前设置
     */
    private void loadSettings() {
        autoStartCheckBox.setSelected(AutoStartupManager.isAutoStartupEnabled());
    }

    /**
     * 保存设置
     */
    private void saveSettings() {
        boolean success = true;
        String errorMessage = "";

        // 处理开机自启动设置
        if (autoStartCheckBox.isSelected()) {
            if (!AutoStartupManager.isAutoStartupEnabled()) {
                success = AutoStartupManager.enableAutoStartup();
                if (!success) {
                    errorMessage = "启用开机自启动失败";
                }
            }
        } else {
            if (AutoStartupManager.isAutoStartupEnabled()) {
                success = AutoStartupManager.disableAutoStartup();
                if (!success) {
                    errorMessage = "禁用开机自启动失败";
                }
            }
        }

        if (success) {
            JOptionPane.showMessageDialog(this, "设置已保存", "提示", JOptionPane.INFORMATION_MESSAGE);
            dispose();
        } else {
            JOptionPane.showMessageDialog(this, errorMessage, "错误", JOptionPane.ERROR_MESSAGE);
        }
    }
}
